<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;


use think\Model;

class ForumNoteCommentModel extends Model
{
    // 确定链接表名
    protected $name = 'forum_note_comment';
    //开启时间戳写入
    protected $autoWriteTimestamp = true;

    /**
     * 插入新的评论
     */
    public function insertComment($param)
    {
        try{
            $result =  $this->allowField(true)->validate('ForumNoteCommentValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return fmsg(-1, '', $this->getError());
            }else{
                return fmsg(0, '', '评论成功!');
            }
        }catch(PDOException $e){

            return fmsg(-2, '', $e->getMessage());
        }
    }

    /**
     * 查询某个帖子所有评论
     */
    public function getCommentList($param)
    {
        if( !empty($param['id']) ){
//            return $this->where(['note_id'=>$param['id']])->order('update_time desc')->select();
            $list = $this->alias('a')
                ->join('z_center c','a.center_id=c.id','LEFT')
                ->where(['a.note_id'=>$param['id']])
                ->order("a.update_time desc")
                ->field('a.id,a.center_id,a.update_time,a.content,a.state,a.is_accept,c.username,c.thumb')
                ->select();
            return $list;
        }

        return [];
    }



    /**
     * 获取单条数据信息
     */
    public function getOneComment($where)
    {
        return $this->where($where)->find();
    }

    /**
     * 设置评论为采纳
     */
    public function setCommentAccept($id)
    {
//        return $this->where('id',$id)->setField('is_accept',1);
        if($this->where('id',$id)->setField('is_accept',1)){
            return fmsg(0, '', '采纳成功!');
        }else{
            return fmsg(1, '', '采纳失败!');
        }
    }


    /**
     * 修改评论
     */
    public function setComment($params)
    {
        if( (!empty($params['id']) && !empty($params['content'])) && $this->where('id',$params['id'])->setField('content',$params['content']) ) {
            return fmsg(0, '', '修改成功!');
        }else{
            return fmsg(1, '', '修改失败!');
        }
    }

    /**
     * 删除评论
     */
    public function delComment($params)
    {
        if( !empty($params['id']) && $this->where('id',$params['id'])->delete() ) {
            return fmsg(0, '', '删除成功!');
        }else{
            return fmsg(1, '', '删除失败!');
        }
    }

    /**
     * 评论周榜
     */
    public function noteCommentTop()
    {
        $w=date('w')? date('w'):7;
        $beginWeek = mktime(0,0,0,date('m'),date('d')-$w+1,date('Y'));//周一
        $endWeek = mktime(0,0,0,date('m'),date('d')-$w+8,date('Y'));//周日

        return $this->alias('a')
            ->join('z_center c','a.center_id=c.id','LEFT')
            ->where('a.create_time','between',[$beginWeek,$endWeek])
            ->group('a.center_id')
            ->field('c.thumb,c.username,count(a.center_id) as comment_count')
            ->order('comment_count desc')
            ->limit(20)
            ->select()->toArray();
    }

    /**
     * 本周热议
     */
    public function noteTop(){
        $w=date('w')? date('w'):7;
        $beginWeek = mktime(0,0,0,date('m'),date('d')-$w+1,date('Y'));//周一
        $endWeek = mktime(0,0,0,date('m'),date('d')-$w+8,date('Y'));//周日
//        $beginWeek =1;//周一
//        $endWeek = 12;//周日
        return $this->alias('a')
            ->join('z_forum_note n','a.note_id=n.id','LEFT')
            ->where('a.create_time','between',[$beginWeek,$endWeek])
            ->group('a.note_id')
            ->field('n.title,n.id,count(a.note_id) as comment_count')
            ->order('comment_count desc')
            ->limit(10)
            ->select()->toArray();
    }


    /**
     * 查询当前用户评论过的文章
     */
    public function getNoteByComment($cid){
        return $this->alias('a')
            ->join('z_forum_note n','a.note_id=n.id','LEFT')
            ->where('a.center_id',$cid)
            ->group('a.note_id')
            ->field('n.title,n.id,a.create_time,a.content')
            ->order('a.create_time desc')
            ->select()->toArray();
    }



}